<!DOCTYPE html>
<script src='http://maps.googleapis.com/maps/api/js?key=AIzaSyC3p82z0b3vMRaVbnplJiNheKfsAyccmJQ&sensor=false'>
</script>
<?php
 include("../include.php");
 $list = sqlreadarray("select itemid,name,latitude,longitude,code from dump.location");
 
 echo getinitialise($list);

 function getinitialise($ans) {
  if (sizeof($ans) < 1) return;
  $lats = array();
  $longs = array();
  $sites = array();
  $sitenames = array();
	$codes = array();
  foreach ($ans as $id => $item) {
   $lats[] = $item[2];
   $longs[] = $item[3];
   $sites[] = '"' . $item[0] . '"';
   $sitenames[] = '"' . str_replace('"', '', $item[1]) . '"';
	 $codes[] = '"'.$item[4].'"';
  }
  $minlat = min($lats);
  $minlong = min($longs);
  $maxlat = max($lats);
  $maxlong = max($longs);
  $centlat = ($minlat + $maxlat) / 2;
  $centlong = ($minlong + $maxlong) / 2;

  $ydist = ($maxlat - $minlat) * 40030 / 360;
  $xdistdeg = cos(deg2rad($centlat)) * 40030 / 360;
  $xdist = ($maxlong - $minlong) * $xdistdeg;

  $scale = max($xdist, $ydist);
	
  $zoom = min((int)(14 - log($scale) / log(2)), 11);
  if (sizeof($ans) == 1) {
   $zoom = 14;
  }
	$zoom = 4;

  $cnt = sizeof($sites);
  $lat = $item[1];
  $long = $item[2];
  $sites = implode(',', $sites);
  $lats = implode(',', $lats);
  $longs = implode(',', $longs);
  $sitenames = implode(',', $sitenames);
	$codes = implode(',',$codes);
  $o = "
<script>
 var lats = [$lats];
 var longs = [$longs];
 var sitenames = [$sitenames];
 var codes = [$codes];
 var sites = [$sites] ;
function initialize() {
 mapOptions = {
  center: new google.maps.LatLng($centlat,$centlong),
  zoom: $zoom,
  mapTypeId: google.maps.MapTypeId.ROADMAP,disableDefaultUI: true
 }
 
 map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
 
 for (var i =0 ; i < $cnt ; i++) {

	var ico = new Object();
  ico.path = google.maps.SymbolPath.FORWARD_CLOSED_ARROW;
  ico.strokeColor = 'red';
  ico.scale = 3;
	
	var m = new Object();
	m.position = new google.maps.LatLng(lats[i],longs[i]);
	m.map = map; 
	m.title = sitenames[i];
	m.icon = ico ;	

  var marker = new google.maps.Marker(m);

	var iw = new Object();
	iw.content = '<a style=color:black href=?itemid=' + sites[i] +  '>' + sitenames[i]+ '</a>' ;
	iw.maxWidth = 100;
  var infowindow = new google.maps.InfoWindow(iw) ;

  attachinfo(marker,infowindow) ;
 }
 
}

function attachinfo(marker,infowindow) {
  google.maps.event.addListener(marker, 'click', function() {infowindow.open(marker.get('map'), marker);});
};
google.maps.event.addDomListener(window, 'load', initialize)
</script>\n";
  return $o;
}


?>
<body>
<div id='map-canvas' style='height: 100% ; width:100% ; position:absolute;left:0px;top:0px;'/></div>
</div>
</body>
</html>